package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.PinYinUtils;
import com.scs.application.modules.fsd.dto.SaveStationDTO;
import com.scs.application.modules.fsd.entity.FsdChest;
import com.scs.application.modules.fsd.entity.FsdStation;
import com.scs.application.modules.fsd.mapper.ChestMapper;
import com.scs.application.modules.fsd.mapper.StationMapper;
import com.scs.application.modules.fsd.service.ChestService;
import com.scs.application.modules.fsd.vo.ChestsAndStationsByUserResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class ChestServiceImpl extends BaseServiceImpl<ChestMapper, FsdChest> implements ChestService {
    @Autowired
    private StationMapper stationMapper;

    @Override
    public FsdChest findById(String id) {
        return this.getOne(
                Wrappers.<FsdChest>query().eq("id", id).last("limit 1")
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createStationAndChest(SaveStationDTO stationDTO) {
        FsdStation station = new FsdStation();
        station.setStationCode(stationDTO.getStationCode());
        station.setStationName(stationDTO.getStationName());
        station.setType(stationDTO.getType());
        station.setStationNamePy(PinYinUtils.getAlpha(stationDTO.getStationName()));
        station.setAutoDownloadSpan(stationDTO.getAutoDownloadSpan());
        station.setAutoLogoutSpan(stationDTO.getAutoLogoutSpan());
        station.setAutoUploadSpan(stationDTO.getAutoUploadSpan());
        station.setAutoCloseDoorSpan(stationDTO.getAutoCloseDoorSpan());
        station.setDoorOpenAlertSpan(stationDTO.getDoorOpenAlertSpan());
        station.setRemark(stationDTO.getRemark());
        stationMapper.insert(station);
        FsdChest chest = new FsdChest();
        chest.setStationId(station.getId());
        chest.setChestNo(stationDTO.getChestNo());
        save(chest);
        return true;
    }

    @Override
    public List<FsdChest> getChestsByStation(String id) {
        return this.list(Wrappers.<FsdChest>query().eq("status", 0).eq("station_id", id).orderByAsc("chest_no"));
    }

    @Override
    public Map<String, String> createChest(String chestNo, String stationId) {
        FsdChest chest = new FsdChest();
        chest.setChestNo(chestNo);
        chest.setStationId(stationId);
        this.save(chest);
        Map<String, String> map = new HashMap<>();
        map.put("id", chest.id);
        return map;
    }

    @Override
    public Boolean editStation(SaveStationDTO stationDTO) {
        FsdStation station = stationMapper.selectOne(Wrappers.<FsdStation>query().eq("id", stationDTO.getId()));
        station.setStationCode(stationDTO.getStationCode());
        station.setStationName(stationDTO.getStationName());
        station.setType(stationDTO.getType());
        station.setStationNamePy(PinYinUtils.getAlpha(stationDTO.getStationName()));
        station.setDoorOpenAlertSpan(stationDTO.getDoorOpenAlertSpan());
        station.setAutoCloseDoorSpan(stationDTO.getAutoCloseDoorSpan());
        station.setAutoUploadSpan(stationDTO.getAutoUploadSpan());
        station.setAutoDownloadSpan(stationDTO.getAutoDownloadSpan());
        station.setAutoLogoutSpan(stationDTO.getAutoLogoutSpan());
        station.setLampCloseSpan(stationDTO.getLampCloseSpan());
        station.setScanTime(stationDTO.getScanTime());
        station.setRemark(stationDTO.getRemark());
        station.setJobNumberMax(stationDTO.getJobNumberMax());
        station.setPasswordMax(stationDTO.getPasswordMax());
        stationMapper.updateById(station);
        return true;
    }

    @Override
    public List<FsdChest> getStationByChest(String id) {
        return baseMapper.getStationByChest(id);
    }

    @Override
    public boolean changeStationAndChestNoByChest(String chestNo, String stationId, String chestId) {
        this.update(findById(chestId),Wrappers.<FsdChest>update().set("station_id", stationId).set("chest_no", chestNo).eq("id",chestId));
        return true;
    }

    @Override
    public List<ChestsAndStationsByUserResult> getChestsAndStationsAll() {
        return baseMapper.getChestsAndStationsAll();
    }

    @Override
    public boolean editChest(String chestNo, String id) {
        FsdChest chest = getOne(Wrappers.<FsdChest>query().eq("id", id).last("limit 1"));
        chest.setChestNo(chestNo);
        this.updateById(chest);
        return true;
    }

    @Override
    public RestResult addChest(String stationId) {
        RestResult result;
        FsdChest chest = new FsdChest();
        chest.setChestNo(this.list(Wrappers.<FsdChest>query().eq("station_id", stationId)).size() + 1 + "");
        chest.setStationId(stationId);
        if (this.save(chest))
            result = RestResult.ok("添加成功");
        else
            result = RestResult.ok("添加失败");
        return result;
    }

    @Override
    public RestResult removeChest(String stationId) {
        RestResult result;
        if(this.remove(new QueryWrapper<FsdChest>().eq("station_id",stationId).orderByDesc("chest_no").last("limit 1")))
            result = RestResult.ok("删除成功");
        else
            result = RestResult.error("删除失败");
        return result;
    }
}
